package com.iemes.controller.motorcade;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.MotorcadeInfoFormMap;
import com.iemes.entity.ResFormMap;
import com.iemes.entity.TransportDriverinfoFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.motorcade.MotorcadeMapper;
import com.iemes.mapper.transport.TransportDriverinfoMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;

@RequestMapping("/motorcadeInfo/")
@Controller
public class MotorcadeInfoController extends BaseController{
	@Inject
	private MotorcadeMapper motorcadeMapper;
	@Inject 
	private TransportDriverinfoMapper transportDriverinfoMapper;
	
	//查看车辆明细列表
		@RequestMapping("/examinemotorcade")
		public String examineMotorcade(Model model){
			model.addAttribute("res",findByRes());
			return Common.BACKGROUND_PATH+"/motorcade/motorcade_list";
		}
		//车辆明细表分页
		@ResponseBody
		@RequestMapping("motorcadeInfo_findbyPage")
		public PageView findByPage(String pageNow, String pageSize) throws Exception 
		{

			MotorcadeInfoFormMap motorcadeInfoFormMap=getFormMap(MotorcadeInfoFormMap.class);
			List<Map<String, Object>> motorcadeInfoFormMaps=motorcadeMapper.getAllInfo(motorcadeInfoFormMap);
			LuogicPaging(pageNow, pageSize, motorcadeInfoFormMaps);
			return pageView;
		}
		
		//修改车队明细表
		@RequestMapping("/motorcade_edit")
		public String motorcadeup(Model model,HttpSession session ){
			String id = getPara("motorcadeInfoFormMap.id");
			
			//获取车队对象
			if(Common.isNotEmpty(id))
			{
				MotorcadeInfoFormMap motorcadeInfoFormMap= baseMapper.findbyFrist("id", id, MotorcadeInfoFormMap.class);
				
				String carBossName= motorcadeInfoFormMap.get("carBossName").toString();
				List<Map<String, Object>> motorcade=motorcadeMapper.getName(carBossName);
				for (Map<String, Object> map : motorcade) {
					if(map.get("id")==motorcadeInfoFormMap.get("id")){
						List<Map<String, Object>> driverName=motorcadeMapper.getAllCarBossId(Integer.valueOf(map.get("id").toString()));
						for (Map<String, Object> map1 : driverName) {
							motorcadeInfoFormMap.put("driverName", map1.get("driverName"));
							motorcadeInfoFormMap.put("driverNumber", map1.get("driverNumber"));
							motorcadeInfoFormMap.put("carBossId", map1.get("carBossId"));
							motorcadeInfoFormMap.put("idd", map1.get("idd"));
						}
					}
				}
				model.addAttribute("motorcade", motorcadeInfoFormMap);
				
				
			}
			return Common.BACKGROUND_PATH + "/motorcade/motorcade_edit";
			
		}
		String id ;
		@RequestMapping("/motorcade_editinfo")
		public String motorcadeupinfo(Model model,HttpSession session ){
			String id = getPara("id");
			this.id=id;
			model.addAttribute("id", id);
			return Common.BACKGROUND_PATH + "/motorcade/motorcade_dateils";
		}
		//车队明细详细分页
		@ResponseBody
		@RequestMapping("/motorcade_dateils_findbyPage")
		public PageView findByPage1(String pageNow, String pageSize,Model model) throws Exception 
		{
			
			//获取对象
			if(Common.isNotEmpty(id))
			{
				MotorcadeInfoFormMap motorcadeInfoFormMap= baseMapper.findbyFrist("id", id, MotorcadeInfoFormMap.class);
				String carBossName= motorcadeInfoFormMap.get("carBossName").toString();
				List<Map<String, Object>> motorcade=motorcadeMapper.getName(carBossName);
				model.addAttribute("motorcade", motorcade);
				model.addAttribute("id", id);
				
				LuogicPaging(pageNow, pageSize, motorcade);
			}
			return pageView;
		}
		
		
		//车队明细详细
			@RequestMapping("/motorcade_dateils")
			public String dateils(String pageNow, String pageSize,Model model,HttpSession session ){
				String id = getPara("id").toString();
				//获取对象
				if(Common.isNotEmpty(id))
				{
					//MotorcadeInfoFormMap motorcadeInfoFormMap= baseMapper.findbyFrist("id", id, MotorcadeInfoFormMap.class);
					MotorcadeInfoFormMap motorcadeInfoFormMap= baseMapper.findbyFrist("id", id, MotorcadeInfoFormMap.class);
					String carBossName= motorcadeInfoFormMap.get("carBossName").toString();
					List<Map<String, Object>> motorcade=motorcadeMapper.getName(carBossName);
					model.addAttribute("motorcade", motorcade);
					model.addAttribute("id", id);
				}
				return Common.BACKGROUND_PATH + "/motorcade/motorcade_dateils";
				
			}
		
		@ResponseBody
		@RequestMapping(value="motorcade_editEntity")
		@SystemLog(module="车队明细",methods="车队-编辑车队")//凡需要处理业务逻辑的.都需要记录操作日志
		@Transactional
		public String editEntity() {
			
			MotorcadeInfoFormMap motorcadeInfoFormMap=getFormMap(MotorcadeInfoFormMap.class);
			String carBossId=motorcadeInfoFormMap.getStr("carBossId");
			TransportDriverinfoFormMap transportDriverinfoFormMap=new TransportDriverinfoFormMap();
			if (Common.isEmpty(carBossId)) {
				transportDriverinfoFormMap.set("driverName", motorcadeInfoFormMap.get("driverName"));
				transportDriverinfoFormMap.set("driverNumber", motorcadeInfoFormMap.get("driverNumber"));
				transportDriverinfoFormMap.set("carBossId", motorcadeInfoFormMap.get("id"));
				try {
					transportDriverinfoMapper.addEntity(transportDriverinfoFormMap);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (Common.isNotEmpty(carBossId)) {
				transportDriverinfoFormMap.put("id", motorcadeInfoFormMap.get("idd"));
				transportDriverinfoFormMap.put("driverName", motorcadeInfoFormMap.get("driverName"));
				transportDriverinfoFormMap.put("driverNumber", motorcadeInfoFormMap.get("driverNumber"));
				
			}
			
			try {
				//transportDriverinfoMapper.editEntity(transportDriverinfoFormMap);
				motorcadeMapper.editEntity(motorcadeInfoFormMap);
				transportDriverinfoMapper.editEntity(transportDriverinfoFormMap);
				
				//transportApplyMapper.editEntity(transportDriverinfoFormMap);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "fasle";
			}
			return "success";
		}
		
		
		//添加车队明细表
		@RequestMapping("motorcade_add")
		public String addUI(Model model){
			List<MotorcadeInfoFormMap> motorcadeInfoFormMap = motorcadeMapper.findByWhere(new MotorcadeInfoFormMap());
			model.addAttribute("motorcadeInfoFormMap", motorcadeInfoFormMap);
			return Common.BACKGROUND_PATH + "/motorcade/motorcade_add";
		}
		
		
		@RequestMapping("/motorcadeInfo_id")
		@ResponseBody
		public MotorcadeInfoFormMap trid(){
			String id=getPara("id");
			MotorcadeInfoFormMap motorcadeInfoFormMap=motorcadeMapper.findbyFrist("id", id, MotorcadeInfoFormMap.class);
			return motorcadeInfoFormMap;
		}
		
		
		@RequestMapping("/driverinfo_id")
		@ResponseBody
		public TransportDriverinfoFormMap tdid(){
			String id=getPara("id");
			TransportDriverinfoFormMap transportDriverinfoFormMap=motorcadeMapper.findbyFrist("id", id, TransportDriverinfoFormMap.class);
			return transportDriverinfoFormMap;
		}
		
		
		
		@ResponseBody
		@RequestMapping("motorcade_addEntity")
		@SystemLog(module="车队明细",methods="添加-添加车队明细")
		@Transactional
		public String addEntity(){
			MotorcadeInfoFormMap motorcadeInfoFormMap=getFormMap(MotorcadeInfoFormMap.class);
			// 获取request
			HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
			// 通过工具类获取当前登录的bean
			UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
			motorcadeInfoFormMap.put("byuser_id", userFormMap.getInt("id"));
			motorcadeInfoFormMap.set("signDate", DateUtils.getStringDateTime());
			motorcadeInfoFormMap.set("status", "1");
			try {
				motorcadeMapper.addEntity(motorcadeInfoFormMap);
				//transportDriverinfoMapper.addEntity(transportDriverinfoFormMap);
			} catch (Exception e) {
				return "false";
			}
			return "success";
		}
		
		//删除车队明细
		@ResponseBody
		@RequestMapping("motorcade_deleteEntity")
		@Transactional(readOnly=false)//需要事务操作必须加入此注解
		@SystemLog(module="删除车队明细",methods="pod按钮-删除pod按钮")//凡需要处理业务逻辑的.都需要记录操作日志
		public String deleteEntity() throws Exception {
			MotorcadeInfoFormMap motorcadeInfoFormMap=getFormMap(MotorcadeInfoFormMap.class);
			String[] ids = getParaValues("ids");
			String arr=ids[0];
			String[] str=arr.split(",");
			for(int i=0;i<str.length;i++){
				motorcadeInfoFormMap.set("status", 2);
				motorcadeInfoFormMap.put("id", str[i]);
				motorcadeMapper.editEntity(motorcadeInfoFormMap);
			}
			
			return "success";
		}
}
